﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MMK.Paket_Tracker.Runtime
{
    /// <summary>
    /// Base class for the model in a MVVM pattern
    /// </summary>
    public class ModelBase : INotifyPropertyChanged, IDataErrorInfo
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="ModelBase"/> class.
        /// </summary>
        public ModelBase()
        {
            ErrorDictionary = new Dictionary<string, string>();
        }
        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo Members

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <value></value>
        /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
        public virtual string Error
        {
            get
            {
                if (ErrorDictionary != null && ErrorDictionary.Count > 0)
                {
                    StringBuilder sBuilder = new StringBuilder();
                    foreach (KeyValuePair<String, String> item in ErrorDictionary)
                    {
                        sBuilder.Append(item.Key);
                        sBuilder.Append(": ");
                        sBuilder.AppendLine(item.Value);
                    }
                    return sBuilder.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }
        }

        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified column name.
        /// </summary>
        /// <value></value>
        public virtual string this[string columnName]
        {
            get { return String.Empty; }
        }

        /// <summary>
        /// Gets or sets the error dictionary.
        /// </summary>
        /// <value>The error dictionary.</value>
        public Dictionary<String,String> ErrorDictionary { get; set; }

        #endregion

    }

}
